<template>
  <div class="file-read-dialog">
    <el-form :model="NodeInputParam.NodeFile" size="mini" label-width="100px" label-position="left">
      <el-form-item label="文件地址">
        <data-source-input
            style="width: 89%;margin-left: 6px"
            :datasource-item="NodeInputParam.NodeFile"
            :node-id="NodeInputParam.NodeFile.DataSourceId"
            :param-name="NodeInputParam.NodeFile.ParamName"
            @change="onChange"
        />
      </el-form-item>
      <el-form-item label="读取行数" width="120px">
        <el-input
            style="width: 90%;"
            v-model="NodeInputParam.NodeFile.Read.ReadLine"
            placeholder="读取的行数，等于0则读全部"
            type="number"
            clearable
        />
      </el-form-item>
      <el-form-item label="文件编码" width="120px">
        <el-select
            v-model="NodeInputParam.NodeFile.EncodingFormat"
            placeholder="请选择地址"
            clearable
            style="width: 90%;"
        >
          <el-option
              v-for="item in encodingFormats"
              :key="item.value"
              :label="item.tip"
              :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="描述">
        <el-input
            v-model="NodeInputParam.Desc"
            type="textarea"
            :rows="3"
            placeholder="请输入描述"
            style="width: 90%"
        ></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>

import FlowNodeDataApi from '@/api/flow/FlowNodeDataApi'
import { AppParams } from '@/utils/appParams'
import Utils from '../../../../utils/Common'
import NodeConfigMixin from '../../../../libs/NodeConfigMixin'
import DataSourceInput from '../../components/NodeInputSourceDataComponent.vue'
import { MessageWarning } from "@/utils";

export default {
  name: 'FileIO',
  mixins: [NodeConfigMixin],
  components: { DataSourceInput },
  data () {
    return {
      encodingFormats: AppParams.COMMON.ENCODING_FORMATS,
      NodeInputParam: {
        NodeFile: {
          Read: {
            ReadLine: 0
          },
          EncodingFormat: null,
          DataSource: 'NULL', DataSourceId: 0, ParamName: null
        },
        Desc: null
      },
    }
  },
  methods: {
    /**
     * 选中的值
     * @param item
     */
    onChange (item) {
      if (!Utils.IsEmpty(item)) {
        this.NodeInputParam.NodeFile.DataSourceId = item.nodeId
        this.NodeInputParam.NodeFile.ParamName = item.ParamName
        this.NodeInputParam.NodeFile.DataSource = item.DataSource
      }
    },
    ShowNodeData () {
      this.SetFormValue(this.currentClickNodeMixin)
    },
    /**
     * 设置回显的表单数据
     * @param _node
     * @constructor
     */
    SetFormValue (_node) {
      this.NodeInputParam = _node.nodeInputParam ?? this.NodeInputParam
    },
    _SaveNode (NodeName) {
      const _file = Object.assign({}, this.NodeInputParam)
      if (_file.Read?.DataSourceId < 0) {
        return MessageWarning('请选择或输入地址')
      }
      new FlowNodeDataApi().Install(this.currentClickNodeMixin.id, _file, NodeName)
    }
  },
  mounted () {
    this.ShowNodeData()
  }
}
</script>

<style lang="less">
.file-read-dialog {
  .file-read-item {
    display: flex;
    margin-bottom: 10px;

    .file-read-item-param-name {
      width: 260px;
    }
  }

  .file-read-adds {
    display: flex;
    flex-direction: row;
    margin-bottom: 10px;

    .file-read-btn {
      height: 20px;
      float: left;
      background-color: red;
    }
  }
}
</style>

